package org.hughie.web;

import org.hughie.batch.BatchJobLauncher;
import org.hughie.common.FundSyncType;
import org.hughie.common.SyncHandlerFactory;
import org.hughie.sync.FundSyncTask;
import org.springframework.web.bind.annotation.*;

/**
 * @author hughie.cheng
 * @date 2024/3/1
 */

@RestController
public class FundSyncController {

    private SyncHandlerFactory factory;

    private BatchJobLauncher launcher;

    public FundSyncController(SyncHandlerFactory factory, BatchJobLauncher launcher) {
        this.factory = factory;
        this.launcher = launcher;
    }

    /**
     * 单个或多个基金信息同步接口
     * @param fundCodes 基金代码
     * @param syncType 同步类型
     */
    @GetMapping("sync")
    void syncBaseInfo(@RequestParam FundSyncType syncType, @RequestParam String[] fundCodes) {
        FundSyncTask handler = factory.getHandler(syncType.getTaskName());
        for (String fundCode : fundCodes) {
            handler.sync(fundCode);
        }
    }

    /**
     * 批量同步接口
     * @param syncType 同步类型
     */
    @GetMapping("batch")
    void syncBatch(@RequestParam FundSyncType syncType) {
        launcher.execute(syncType.getTaskName());
    }

    @GetMapping("fund-list")
    void initFundList() {
        launcher.initFundList();
    }

}
